<?php
function xmlGetFnMsg ($str)
{
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type: text/plain");
    echo "{\"errormsg\":\"{$str}\"}";
    exit();
}

if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
$gameArr = array(29 => "五不中", 39 => "六不中", 40 => "七不中", 41 => "八不中", 42 => "九不中", 43 => "十不中", 44 => "十一不中", 45 => "十二不中");
$showArr = array();
$betStr = getVar("betStr");
if ($betStr != "")
{
    $betStrArr = explode(",", $betStr);
}
else
{
    $betStrArr = array();
}
$lostwin = array();
$showNumArr = array();
foreach ($betStrArr as $val)
{
    $val = intval($val);
    if (isset($gameArr[$val]))
    {
        $showArr[] = $val;
        $showNumArr[$val] = array();
    }
}
$countShowNum = count($showArr);
if ($countShowNum < 1)
{
    xmlGetFnMsg("-1, error bettype");
}
$bettypeStr = implode(",", $showArr);
$showRateFlag = getVar("showRateFlag") == 1 ? true : false;
$showDataN = intval(getVar("showDataN"));
$showDataN = $showDataN == 0 ? 1200 : $showDataN;
$lotto_id = intval(getVar("lotto_id"));
if ($lotto_id < 1)
{
    $rs = getdb("select id from lotto where open>='" . date("Y-m-d") . "' order by open");
    if ($rs->eof)
    {
        $ui->showErrorPage("<p>目前没有可以下注期数！</p>");
        exit();
    }
    $lotto_id = $rs->f['id'];
}
$split = intval(getVar("split"));
$split = $split == 0 ? 0 : 1;
$userid = $curruser['id'];
$theacl = $curruser['acl'];
$g_disk_code = array(1 => "a", 2 => "b", 3 => "c", 4 => "d");
$disk = intval(getVar("disk"));
$disk = isset($g_disk_code[$disk]) ? $disk : 0;
$diskFlag = $disk == 0 ? true : false;
if ($countShowNum == 1)
{
    $sqlBettype = "bettype = {$bettypeStr} ";
}
else
{
    $sqlBettype = "bettype IN ({$bettypeStr}) ";
}
$sql = "SELECT * FROM bet WHERE {$sqlBettype} AND lotto_id = {$lotto_id} AND rame IN (0,9)";
if ($theacl != 255)
{
    $userlist = GetUserList($userid);
    if ($userlist == "")
    {
        xmlGetFnMsg("-3, empty member");
    }
    $sql .= " AND user_id IN ({$userlist})";
}
$disk_code = "1";
if (0 < $disk)
{
    $sql .= " AND disk = " . $disk;
    $disk_code = $disk;
}
$sql .= " ORDER BY unitmoney DESC, bettype, numbers";
$percent = 0;
$totalamount = array();
$totalcount = array();
$ary = array();
foreach ($showArr as $sVal)
{
    $totalamount[$sVal] = 0;
    $totalcount[$sVal] = 0;
    $ary[$sVal] = array();
}

$rs = getdb($sql);
while (!$rs->eof)
{
    if ($split == 1)
    {
        switch ($theacl)
        {
            case 2 :
                $percent = $rs->f['mpercent'];
                break;
            case 3 :
                $percent = $rs->f['percent2'];
                break;
            case 4 :
                $percent = $rs->f['percent3'];
                break;
            case 5 :
                $percent = $rs->f['percent4'];
                break;
            default :
                $percent = $rs->f['upercent'];
                break;
        }
        if (0 < $percent)
        {
            $percent /= 100;
        }
    }
    else
    {
        $percent = 1;
    }
    $bettype = $rs->f['bettype'];
    $line_nums = $rs->f['line_nums'];
    $tmp = $percent * $rs->f['unitmoney'];
	
    $srcFlag = array_search($line_nums, $showNumArr[$bettype]);
    if ($srcFlag === false)
    {
        $ary[$bettype][] = array("num" => $line_nums, "amount" => $tmp);
        $showNumArr[$bettype][] = $line_nums;
    }
    else
    {
        $ary[$bettype][$srcFlag]['amount'] ++;
    }
    ++$totalcount[$bettype];
    $totalamount[$bettype] ++;
    $rs->movenext();
}

$rs->close();
if ($diskFlag && 2 < $theacl)
{
    if ($theacl == 255)
    {
        $sql = "SELECT * FROM bet2 WHERE lotto_id = {$lotto_id} AND {$sqlBettype} AND rame IN (0,9)";
    }
    else
    {
        $sql = "SELECT * FROM bet2 WHERE lotto_id = {$lotto_id} AND uperid{$theacl} = {$userid} AND {$sqlBettype} AND rame in (0,9)";
    }
    $rs = getdb($sql);
    while (!$rs->eof)
    {
        $percent = 1;
        $backs = 100;
        switch ($theacl)
        {
            case 255 :
                $backs = $rs->f['back5'];
                $percent = $rs->f['percent255'] - 0;
                break;
            case 5 :
                $backs = $rs->f['back4'];
                $percent = $rs->f['percent5'];
                break;
            case 4 :
                $backs = $rs->f['back3'];
                $percent = $rs->f['percent4'];
                break;
            case 3 :
                $backs = $rs->f['back2'];
                $percent = $rs->f['percent3'];
                break;
        }
        if (0 < $percent)
        {
            $percent /= 100;
        }
        if ($split != 1)
        {
            $percent = 1;
        }
        $bettype = $rs->f['bettype'];
        $line_nums = $rs->f['line_nums'];
        $tmp = $rs->f['unitmoney'] * $percent;
		
        $srcFlag = array_search($line_nums, $showNumArr[$bettype]);
        if ($srcFlag === false)
        {
            $ary[$bettype][] = array("num" => $line_nums, "amount" =>$tmp);
            $showNumArr[$bettype][] = $line_nums;
        }
        else
        {
            $ary[$bettype][$srcFlag]["amount"]+=$tmp;
        }
        ++$totalcount[$bettype];
        $totalamount[$bettype] ++;
        $rs->movenext();
    }
    $rs->close();
}

if ($diskFlag && $theacl == 255)
{
    $sql = "SELECT * FROM bet8 WHERE lotto_id = {$lotto_id} AND {$sqlBettype} AND rame IN (0,9)";
    $rs = getdb($sql);
    while (!$rs->eof)
    {
        $bettype = $rs->f['bettype'];
        $line_nums = $rs->f['line_nums'];
        $tmp = $rs->f['amount'];
        $srcFlag = array_search($line_nums, $showNumArr[$bettype]);
        if ($srcFlag === false)
        {
            $ary[$bettype][] = array("num" => $line_nums, "amount" => 0 - $tmp);
            $showNumArr[$bettype][] = $line_nums;
        }
        else
        {
            $ary[$bettype][$srcFlag]["amount"] -=$tmp;;
        }
        ++$totalcount[$bettype];
        $totalamount[$bettype] --;
        $rs->movenext();
    }
}
else if ($diskFlag)
{
    $sql = "SELECT * FROM bet2 WHERE lotto_id = {$lotto_id} AND user_id = {$userid} AND {$sqlBettype} AND rame IN (0,9)";
    $rs = getdb($sql);
    while (!$rs->eof)
    {
        $backs = 100;
        switch ($theacl)
        {
            case 5 :
                $backs = $rs->f['back5'];
                break;
            case 4 :
                $backs = $rs->f['back4'];
                break;
            case 3 :
                $backs = $rs->f['back3'];
                break;
            case 2 :
                $backs = $rs->f['back2'];
                break;
        }
        $bettype = $rs->f['bettype'];
        $line_nums = $rs->f['line_nums'];
        $tmp = $rs->f['unitmoney'];
        $srcFlag = array_search($line_nums, $showNumArr[$bettype]);
        if ($srcFlag === false)
        {
            $ary[$bettype][] = array("num" => $line_nums, "amount" => 0 - $tmp);
            $showNumArr[$bettype][] = $line_nums;
        }
        else
        {
           
		   $ary[$bettype][$srcFlag]["amount"] -=$tmp;;
        }
        ++$totalcount[$bettype];
        $totalamount[$bettype] --;
        $rs->movenext();
    }
}
$rs->close();

$showStr = "";
foreach ($showArr as $sKey)
{
    if ($showDataN == 0)
    {
        break;
    }
    $showStr .= ",\"bet" . $sKey . "\":{";
    if ($showRateFlag)
    {
        $rs = getdb("SELECT * FROM rate29 WHERE lotto_id = {$lotto_id} AND bettype = {$sKey} AND disk = {$disk_code} LIMIT 1");
        if ($rs->eof)
        {
            xmlGetFnMsg("-4, system maintenance");
        }
        $showStr .= "\"rate\":[";
        $i = 1;
        while ($i < 49)
        {
            $tempI = str_pad($i, 2, "0", STR_PAD_LEFT);
            $rate = $rs->f["r" . $tempI];
            $showStr .= $rate . ",";
            ++$i;
        }
        $showStr .= $rs->f['r49'];
        $showStr .= "],";
    }
    $lostwin[$sKey] = array();
	
    foreach ($ary[$sKey] as $lKey => $lVal)
    {
        $lostwin[$sKey][$lKey] = 0 - $lVal['amount'];
    }
    asort($lostwin[$sKey]);
    reset($lostwin[$sKey]);
    $showStrAmount = "";
    $showStrNum = "";
    foreach ($lostwin[$sKey] as $lKey => $lVal)
    {
        $showStrAmount .= "," . intval($ary[$sKey][$lKey]['amount']);
        $showStrNum .= ",\"" . $ary[$sKey][$lKey]['num'] . "\"";
    }
	
    $showStrAmount = $showStrAmount == "" ? "" : substr($showStrAmount, 1);
    $showStrNum = $showStrNum == "" ? "" : substr($showStrNum, 1);
    $showStr .= "\"amount\":[" . $showStrAmount . "],";
    $showStr .= "\"numbers\":[" . $showStrNum . "],";
    $showStr .= "\"total\":\"" . $totalamount[$sKey] . "\",";
    $showStr .= "\"totalNum\":\"" . $totalcount[$sKey] . "\"";
    $showStr .= "}";
    --$showDataN;
}
header("Cache-Control: no-cache, must-revalidate");
header("Content-Type: text/plain");
echo "{\"errormsg\":\"1\",\"betArr\":[" . $bettypeStr . "]" . $showStr . "}";
exit();
?>